<?php
declare (strict_types = 1);

namespace App\Admin\Controller;

use App\Common\Controller\AbstractController;
use App\Common\Logic\CommonLogic;
use App\Server\Model\Cate;
use App\Server\Model\CateGroup;
use App\Server\Model\Content;
use App\Server\Model\ContentGroup;
use Hyperf\Di\Annotation\Inject;
use App\Admin\Validate\ContentValidate;

class ContentController extends AbstractController
{

    /**
     * @Inject()
     * @var ContentValidate
     */
    protected $ContentValidate;

    public function contentIndex()
    {
        $page = $this->request->query('page');
        if ($page)
        {
            $param = $this->request->all();
            $where = [];
            if (!empty($param['cate_id'])){
                $where[] = ['cate_id','=',$param['cate_id']];
            }
            $contentModel = new Content();
            $contentList = $contentModel->getListPage($where,['*'],'sort,created_at desc');
            foreach ($contentList['data'] as &$v){
                $v['cate_name']   = '分类';
            }
            return $this->success($contentList['data'], '成功', ['count'=>$contentList['total']]);
        }
        else
        {
            $data = [];
            $cates = (new Cate())->getList([],['*']);
            $data['cate'] = (new CommonLogic())->taglist($cates);
            return $this->render->render('admin/content/content_index',$data);
        }
    }

    public function addContent()
    {
        $contentModel = new Content();
        $page = $this->request->query('page');
        if ($page) {
            $param = $this->request->all();
            unset($param['page']);
            $errorMessage = $this->ContentValidate->addContent($this->request);
            //验证部分数据合法性
            if ($errorMessage) {
                return $this->error('提交失败：' . $errorMessage);
            }
            unset($param['file']);
            $param['created_at'] =time();
            $addRes = $contentModel->dataAdd($param);
            if ($addRes){
                return $this->success([],'创建成功');
            }else{
                return $this->error('创建失败');
            }
        }
        else
        {
            $data = [];
            $cates = (new Cate())->getList([],['*']);
            $data['cate'] = (new CommonLogic())->taglist($cates);
            return $this->render->render('/admin/content/add_content',$data);
        }

    }




    public function editContent()
    {
        $id = $this->request->query('id');
        $contentModel = new Content();
        $cate = new Cate();
        $page = $this->request->query('page');
        if ($page) {
            $param = $this->request->all();
            unset($param['page']);
            $errorMessage = $this->ContentValidate->editContent($this->request);
            //验证部分数据合法性
            if ($errorMessage) {
                return $this->error('提交失败：' . $errorMessage);
            }
            unset($param['file']);
            $addRes = $contentModel->dataUpdate([['id','=',$param['id']]],$param);
            if ($addRes){
                return $this->success([],'修改成功');
            }else{
                return $this->error('修改失败');
            }
        }
        else
        {
            $data = [];
            $data['data'] = $contentModel -> getInfo([['id','=',$id]],['*']);
            $cates = $cate->getList([],['*']);
            $data['cate'] = (new CommonLogic())->taglist($cates);
            return $this->render->render('/admin/content/edit_content', $data);
        }
    }

    public function deleteContent()
    {
        $contentModel = new Content();
        $id = $this->request->query('id');
        if(!$id) {
            return $this->error('缺少必要条件');
        }
        $deleteRes = $contentModel->dataDelete([['id','=',$id]]);
        if (!$deleteRes) {
            return $this->error('删除失败！');
        }
        return $this->success([],'删除成功！');
    }


}
